﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Fawcett.Plugins.Controls
{
    public partial class BAI : UserControl
    {

        #region Fields

        // Parameters
        double PitchAngle = 0; // Phi
		double RollAngle = 0; // Theta

        // Images
        Bitmap bmpBackground = new Bitmap(Images.Horizon_Background);
        Bitmap bmpHorizon = new Bitmap(Images.Horizon_GroundSky);
        Bitmap bmpAvion = new Bitmap(Images.Maquette_Avion);

        #endregion

        #region Contructor


        public BAI()
		{
			// Double bufferisation
			SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint |
				ControlStyles.AllPaintingInWmPaint, true);
        }

        #endregion

        #region Paint

        protected override void OnPaint(PaintEventArgs pe)
        {
            // Calling the base class OnPaint
            base.OnPaint(pe);

            // Pre Display computings

            Point ptBoule = new Point(25, - 210);
            Point ptRotation = new Point(150, 150);

            float scale = (float)this.Width / bmpBackground.Width;

            bmpBackground.MakeTransparent(Color.Yellow);
            bmpAvion.MakeTransparent(Color.Yellow);

            // display Horizon
            Common.RotateAndTranslate(pe, bmpHorizon, RollAngle, 0, ptBoule, (int)(4*PitchAngle), ptRotation, scale);

            // diplay mask
            Pen maskPen = new Pen(this.BackColor,30*scale);
            pe.Graphics.DrawRectangle(maskPen, 0, 0, bmpBackground.Width * scale, bmpBackground.Height * scale);

            // display cadran
            pe.Graphics.DrawImage(bmpBackground, 0, 0, (float)(bmpBackground.Width * scale), (float)(bmpBackground.Height * scale));

            // display aircraft symbol
            pe.Graphics.DrawImage(bmpAvion, (float)((0.5 * bmpBackground.Width - 0.5 * bmpAvion.Width) * scale), (float)((0.5 * bmpBackground.Height - 0.5 * bmpAvion.Height) * scale), (float)(bmpAvion.Width * scale), (float)(bmpAvion.Height * scale));
        }

        #endregion

        #region Methods

        /// <summary>
        /// Define the physical value to be displayed on the indicator
        /// </summary>
        /// <param name="aircraftPitchAngle">The aircraft pitch angle in °deg</param>
        /// <param name="aircraftRollAngle">The aircraft roll angle in °deg</param
        public void SetAttitudeIndicatorParameters(double aircraftPitchAngle, double aircraftRollAngle)
        {
            PitchAngle = aircraftPitchAngle;
            RollAngle = aircraftRollAngle * Math.PI / 180;

            this.Refresh();
        }
        #endregion
    }
}
